package com.xiaoyu.lanling.feature.profile.activity

import android.app.ProgressDialog
import android.os.Bundle
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.upload.UploadListener
import com.xiaoyu.base.utils.upload.UploadMediaType
import com.xiaoyu.base.utils.upload.UploadParam
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.event.audio.AudioResultEvent
import com.xiaoyu.lanling.event.user.UserUploadVoiceEvent
import com.xiaoyu.lanling.event.user.UserVoiceTemplateTextEvent
import com.xiaoyu.lanling.feature.user.data.UserInfoData
import com.xiaoyu.lanling.util.upload.UploadCategory
import com.xiaoyu.lanling.widget.audio.AudioRecordViewFragment
import kotlinx.android.synthetic.main.activity_voice_record.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class UserVoiceRecordActivity : AppCompatToolbarActivity() {

    companion object {
        const val SHORTEST_DURATION_IN_S = 5
    }

    private val requestTag = Any()

    private lateinit var mAudioRecordViewFragment: AudioRecordViewFragment
    private var mProgressDialog: ProgressDialog? = null

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_voice_record)
        initToolbar()

        initView()
        initBind()
        initEvent()
        initData()
        initAudioRecord()
    }

    private fun initView() {
        title = getString(R.string.voice_record_title)
    }

    private fun initBind() {
        change_desc.setOnClickDebounceListener {
            getVoiceTemplateText()
        }
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: UserVoiceTemplateTextEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                card_desc.text = event.text
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: AudioResultEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                checkAudio(event)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: UserUploadVoiceEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                dismissProgressDialog()

                if (event.isFailed) {
                    return
                }
                finish()
            }
        })
    }

    private fun checkAudio(event: AudioResultEvent) {
        if (event.getDurationInSecond() < SHORTEST_DURATION_IN_S) {
            GlobalUI.getInstance().showToast(R.string.user_voice_record_too_short_toast)
            return
        }
        uploadAudio(event)
    }

    private fun uploadAudio(event: AudioResultEvent) {
        showProgressDialog()

        UploadParam(event.filePath, UploadCategory.USER_VOICE, UploadMediaType.AUDIO, "").upload(object : UploadListener() {
            override fun onSuccess(url: String) {
                UserInfoData.uploadVoice(requestTag, url, event.getDurationInSecond())
            }
        })
    }

    private fun showProgressDialog() {
        mProgressDialog = ProgressDialog(this)
        mProgressDialog?.setMessage(AppContext.getString(R.string.common_loading_please_wait))
        mProgressDialog?.show()
    }

    private fun dismissProgressDialog() {
        mProgressDialog?.dismiss()
    }

    private fun initData() {
        getVoiceTemplateText()
    }

    private fun getVoiceTemplateText() {
        UserInfoData.getVoiceTemplateText(requestTag)
    }

    private fun initAudioRecord() {
        if (isDestroyed) {
            return
        }
        mAudioRecordViewFragment = AudioRecordViewFragment.newInstance()
        mAudioRecordViewFragment.setRequestTag(requestTag)
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.voice_record_layout, mAudioRecordViewFragment)
                .commitAllowingStateLoss()
    }
}